package com.sky.wsp.solr.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.wsp.solr.service.SolrService;
import com.sky.wsp.solr.vo.ProjectItemDto;
import com.sky.wsp.solr.vo.ProjectItemEntity;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service("solrService")
public class SolrClientServiceImpl implements SolrService {

    @Resource
    private SolrClient solrClient;

    // solr 中定义的索引库名称
    private final String INDEX_LIBRARY_NAME = "devops_core";

    @Override
    public void update(ProjectItemEntity projectItemEntity) {
        try {
            solrClient.addBean(INDEX_LIBRARY_NAME, projectItemEntity);
            solrClient.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateBatch(List<ProjectItemEntity> projectItemEntities) {
        try {
            solrClient.addBean(INDEX_LIBRARY_NAME, projectItemEntities);
            solrClient.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(List<String> ids) {
        try {
            solrClient.deleteById(INDEX_LIBRARY_NAME, ids);
            solrClient.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public IPage<ProjectItemEntity> queryList(Integer pageIndex, Integer pageSize, String itemKey) {
        SolrQuery solrQuery = new SolrQuery("itemKey:" + itemKey);
        solrQuery.setStart((pageIndex - 1) * pageSize);
        solrQuery.setRows(pageSize);
        solrQuery.setSort("startTime", SolrQuery.ORDER.asc);
        //开户高亮
        solrQuery.setHighlight(true);
        //设置要进行高亮的属性
        solrQuery.addHighlightField("title");
        //设置高亮的前后缀
        solrQuery.setHighlightSimplePre("<font color=red>");
        solrQuery.setHighlightSimplePost("</font>");
        QueryResponse response = null;
        try {
            response = solrClient.query(INDEX_LIBRARY_NAME, solrQuery);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (null == response) {
            return null;
        }
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        for (Map.Entry<String, Map<String, List<String>>> stringMapEntry : highlighting.entrySet()) {
            String id = stringMapEntry.getKey();
            for (Map.Entry<String, List<String>> stringListEntry : stringMapEntry.getValue().entrySet()) {
                String prop = stringListEntry.getKey();
                String val = stringListEntry.getValue().get(0);
                System.out.println(String.format("id: %s, %s: %s", id, prop, val));
            }
        }

        SolrDocumentList results = response.getResults();
        List<ProjectItemEntity> records = response.getBeans(ProjectItemEntity.class);
        records.forEach(e -> {
            Map<String, List<String>> map = highlighting.get(e.getId());
            if(null == map){
                return;
            }
            List<String> title = map.get("title");
            if(null == title || title.isEmpty()){
                return;
            }
            e.setTitle(title.get(0));
        });

        IPage<ProjectItemEntity> page = new Page<>(pageIndex, pageSize);
        page.setTotal(results.getNumFound());
        page.setRecords(records);
        return page;
    }

    @Override
    public IPage<ProjectItemDto> queryHighlightList(Integer pageIndex, Integer pageSize, String itemKey) {
        return null;
    }
}
